package com.shoutao.website.controller.datacontroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shoutao.website.contact.*;
import com.shoutao.website.entity.Productpicinfo;
import com.shoutao.website.entity.Userinfo;
import com.shoutao.website.mapper.ProductpicinfoMapper;
import com.shoutao.website.mapper.UserinfoMapper;
import com.shoutao.website.utils.ObjectUtils;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @Description:
* @Param:
* @return:
* @Author: cqin
* @Date: 2019/11/5
*/
@RestController
@RequestMapping(value = "/api/image")
public class ImageController {

    @Autowired
    private ProductpicinfoMapper productpicinfoMapper;

    public static Map<String, Productpicinfo> fileMap = new HashMap<>();


    @ApiOperation(value = "图片接口", notes = "图片接口")
    @RequestMapping(value = "/productImage.do", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "imageName", value = "图片名称", dataType = "String", required = true)
    })
    public String productImage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,  String imageName) throws SQLException, IOException {
        ImageResponse imageResponse = new ImageResponse();

        Productpicinfo productpicinfo = null;
        if(fileMap.containsKey(imageName)){
            productpicinfo = fileMap.get(imageName);
        }else {
            Productpicinfo query = new Productpicinfo();
            query.setPicname(imageName);
            List<Productpicinfo> productpicinfoList = productpicinfoMapper.selectList(new QueryWrapper<>(query));
            if(CollectionUtils.isNotEmpty(productpicinfoList)){
                productpicinfo = productpicinfoList.get(0);
                fileMap.put(imageName, productpicinfo);
            }
        }

        if(productpicinfo != null){
            byte[] img = new sun.misc.BASE64Decoder().decodeBuffer(productpicinfo.getPicdata().split(",")[1]);
            httpServletResponse.setContentType("image/png");
            OutputStream os = httpServletResponse.getOutputStream();
            os.write(img);
            os.flush();
            os.close();
            return "success";
        }
        return "";

    }
}
